﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class TR_ScholarshipExpense : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        InitialData_BLL iniData = new InitialData_BLL();
        private TrainingScholarshipExpenseBLL TR_ScholaExp = new TrainingScholarshipExpenseBLL();
        private SsMng creatby = new SsMng();
        private Main main = new Main();
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }
        
        private void LoadDataGrid()
        {
            try
            {
                TrainingScholarshipExpenseBLL TR_ScholaExp = new TrainingScholarshipExpenseBLL();
                DataSet dsExp;
                dsExp = TR_ScholaExp.GetData("0");

                RadGrid2.DataSource = dsExp.Tables[0] ;
                RadGrid2.DataBind();

            }catch(Exception ex){

            }           
        }

        protected void RadGrid2_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddData")
            {
                UpdateUI("AddNew");
                ClearControl();
                PanelAddData.Visible = true;
                PanelGrid.Visible = false;
            }
            if (e.CommandName == "Delete")
            {
                GridDataItem item = (GridDataItem)e.Item;
                txtExpSeq.Text = item["exp_seq"].Text;
                string exp_seq = txtExpSeq.Text;
                TR_ScholaExp.Delete(exp_seq, creatby.EmpSeq);

                LoadDataGrid();
                UpdateUI("View");

                //int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Sequence"]);
                //iniData.DeleteScholarshipExpSupport(seq, "1");
            }
            if (e.CommandName == "ExpandCollapse")
                return;
            if (e.CommandName == "DataEdit")
            {
                GridDataItem item = (GridDataItem)e.Item;
                txtExpSeq.Text = item["exp_seq"].Text;
                txtExpDescr.Text = item["exp_descr"].Text;
                UpdateUI("Update");
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string exp_seq = txtExpSeq.Text;
            string exp_descr = txtExpDescr.Text;

            TR_ScholaExp.Update(exp_seq, exp_descr, creatby.EmpSeq);

            LoadDataGrid();
            UpdateUI("View");
            //try
            //{
            //    TrainingScholarshipExpenseBLL TR_ScholaExp = new TrainingScholarshipExpenseBLL();

            //}
            //catch (Exception ex)
            //{

            //}            
            //int ScholaTypeSeq = Convert.ToInt32(txtScholaTypeSeq.Text);
            //string strScholaType = txtScholaType.Text;
            //int strCountry = Convert.ToInt32(rblCoutry.SelectedValue);
            //int UserID = ssMng.EmpSeqI;
            //DateTime dtNow = DateTime.Now;
            //UpdateUI("Save");
            //TrainingScholarshipTypeInfo info = new TrainingScholarshipTypeInfo();
            //info.scholar_type_seq = ScholaTypeSeq;
            //info.scholar_type_descr = strScholaType;
            //info.scholar_local = strCountry;
            //info.updated_by = UserID;
            //info.updated_when = dtNow;
            //SchTypeBLL.Update(info);
            //LoadDataGrid();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate() == true)
            {
               // TrainingScholarshipExpenseBLL TR_ScholaExp = new TrainingScholarshipExpenseBLL();
                string exp_descr = txtExpDescr.Text;
                UpdateUI("Save");
                ClearControl();

                TR_ScholaExp.Insert(exp_descr, creatby.EmpSeq);

                LoadDataGrid();
                UpdateUI("View");
            }

        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI("Cancel");
            ClearControl();
            LoadDataGrid();
        }
        protected void ClearControl()
        {
            txtExpDescr.Text = "";
            //    rblCoutry.SelectedValue = "1";
            //    lblError.Text = "";
            //    txtScholaType.Text = "";
            //    txtScholaTypeSeq.Text = "";
        }
        protected bool checkValidate()
        {
            bool bOK = true;
            //string strScholaType = txtScholaType.Text;
            //if (strScholaType.Length == 0)
            //{
            //    bOK = false;
            //    lblError.Text = "* กรุณาใส่แหล่งทุน";
            //}
            return bOK;
        }
        protected void UpdateUI(string strCommand)
        {
            switch (strCommand)
            {
                case "Save":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "AddNew":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnUpdate.Visible = false;
                    btnSave.Visible = true;

                    break;
                case "Cancel":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;

                    break;

                case "Update":
                    PanelGrid.Visible = false;
                    PanelAddData.Visible = true;
                    btnSave.Visible = false;
                    btnUpdate.Visible = true;

                    break;
                case "View":
                    PanelGrid.Visible = true;
                    PanelAddData.Visible = false;
                    break;
            }
        }
    }
}
